package com.wenan.ui.controls.flashMaps
{
	import com.wenan.data.CityCollection;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;

	public class MapSprite extends UIComponent
	{
		private var _cityArray:CityCollection;
		private var _provinceArray:ArrayCollection;
		
		public function MapSprite()
		{
			initilizeComponents();
			
			registActions();
		}
		
		public function get provinceArray():ArrayCollection
		{
			return _provinceArray;
		}

		public function set provinceArray(value:ArrayCollection):void
		{
			_provinceArray = value;
		}

		public function get cityArray():CityCollection
		{
			return _cityArray;
		}

		public function set cityArray(value:CityCollection):void
		{
			_cityArray = value;
		}

		protected function initilizeComponents():void
		{
			
		}
		
		protected function getProvinceMovieClips():Array
		{
			return null;
		}
		
		protected function stopAll(c:DisplayObjectContainer):void 
		{
			var me:DisplayObject;
			for(var i:uint = 0; i<c.numChildren; i++) {
				me = c.getChildAt(i);
				if(me is MovieClip) {
					(me as MovieClip).stop();
				}
			}
		}
		
		private function registActions():void 
		{
			var mc:MovieClip;
			var mcs:Array = getProvinceMovieClips();
			
			for(var i:uint = 0; i<mcs.length; i++) 
			{
				mc = mcs[i];
				mc.alpha = 1;
				mc.buttonMode = true;
				mc.addEventListener(MouseEvent.MOUSE_OVER, mapOverHandler);
				mc.addEventListener(MouseEvent.MOUSE_OUT, mapOutHandler);
				mc.addEventListener(MouseEvent.CLICK, mapClipHandler);
			}
		}
		
		protected function mapOverHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			for(var i:int=0;i<provinceArray.length;i++)
			{
				var obj:Object=provinceArray.getItemAt(i);
				if(obj.proviceSpell==mc.name)
					mc.gotoAndStop(2);
			}
		}
		
		protected function mapOutHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			mc.gotoAndStop(1);
		}
		
		protected function mapClipHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			dispatchEvent(new MapEvent(MapEvent.CLICK_PROVINCE, mc));
		}
	}
}